﻿Imports System.Data.OleDb


Public Class frmThongTinKhachHang
    Inherits System.Windows.Forms.Form


    Private Sub frmThongTinKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadKhachHang()
    End Sub

    Private Sub LoadKhachHang()
        Dim kq As New DataTable
        Dim Loai_ket_noi As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
        Dim CSDL As String = "Data Source=QuanLyPhongTro.mdb"
        Dim Chuoi_ket_noi As String = Loai_ket_noi & CSDL
        Dim Ket_noi As New OleDbConnection(Chuoi_ket_noi)
        Dim Bo_thich_ung As New OleDbDataAdapter("select * from KhachHang", Ket_noi)
        Bo_thich_ung.FillSchema(kq, SchemaType.Source)
        Bo_thich_ung.Fill(kq)
        DataGrid1.DataSource = kq
        Ket_noi.Close()
    End Sub

    Private Sub btnThoat_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If (MessageBox.Show("Bạn có chắc chắn muốn thoát không?", "Thoát", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
            Global.System.Windows.Forms.Application.Exit()
        End If
    End Sub

   
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Try
            Dim kh As New KhachHangDTO()
            kh.TenKhach = txtTenKhach.Text
            kh.MaLoaiKhach = txtMaLoaiKhach.Text
            kh.CMND = txtCMND.Text
            kh.DiaChi = txtDiaChi.Text
            kh.NgaySinh = dtpNgaySinh.Value
            If (rboNam.Checked) Then
                kh.GioiTinh = "Nam"
            ElseIf (rboNu.Checked) Then
                kh.GioiTinh = "Nu"
            End If

            Dim str = String.Format("insert into KHACHHANG (TenKhach, MaLoaiKhach, CMND, DiaChi, GioiTinh, NgaySinh) values ('{0}',{1},'{2}','{3}','{4}','{5}')",
                                    kh.TenKhach, kh.MaLoaiKhach, kh.CMND, kh.DiaChi, kh.GioiTinh, kh.NgaySinh)
            Dim dInserKH As New SQLProvider()
            Dim iKQ As Integer = -1
            iKQ = dInserKH.ExecNoneQuery(str)

            If (iKQ < 1) Then
                MessageBox.Show("Không thể thêm mới khách hàng !")
            Else
                LoadKhachHang()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Try
            Dim kh As New KhachHangDTO()
            kh.MaKhach = txtMaKhach.Text

            Dim str = String.Format("delete from KHACHHANG where MaKhach={0}", kh.MaKhach)
            Dim dInserKH As New SQLProvider()
            Dim iKQ As Integer = -1
            iKQ = dInserKH.ExecNoneQuery(str)

            If (iKQ < 1) Then
                MessageBox.Show("Không thể xóa khách hàng !")
            Else
                LoadKhachHang()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Try
            Dim kh As New KhachHangDTO()
            kh.MaKhach = txtMaKhach.Text
            kh.TenKhach = txtTenKhach.Text
            kh.MaLoaiKhach = txtMaLoaiKhach.Text
            kh.CMND = txtCMND.Text
            kh.DiaChi = txtDiaChi.Text
            kh.NgaySinh = dtpNgaySinh.Value
            If (rboNam.Checked) Then
                kh.GioiTinh = "Nam"
            ElseIf (rboNu.Checked) Then
                kh.GioiTinh = "Nu"
            End If

            Dim str = String.Format("update KHACHHANG set TenKhach='{0}',MaLoaiKhach={1},CMND='{2}',DiaChi='{3}',GioiTinh='{4}',NgaySinh='{5}' where MaKhach={6}",
                                    kh.TenKhach, kh.MaLoaiKhach, kh.CMND, kh.DiaChi, kh.GioiTinh, kh.NgaySinh, kh.MaKhach)
            Dim dInserKH As New SQLProvider()
            Dim iKQ As Integer = -1
            iKQ = dInserKH.ExecNoneQuery(str)

            If (iKQ < 1) Then
                MessageBox.Show("Không thể sửa khách hàng !")
            Else
                LoadKhachHang()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub DataGrid1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectionChanged
        Try
            If (DataGrid1.Rows.Count > 0 And DataGrid1.SelectedRows.Count > 0) Then
                txtMaKhach.Text = DataGrid1.SelectedRows(0).Cells(0).Value
                txtTenKhach.Text = DataGrid1.SelectedRows(0).Cells(1).Value
                txtMaLoaiKhach.Text = DataGrid1.SelectedRows(0).Cells(2).Value
                txtCMND.Text = DataGrid1.SelectedRows(0).Cells(3).Value
                txtDiaChi.Text = DataGrid1.SelectedRows(0).Cells(4).Value
                If (DataGrid1.SelectedRows(0).Cells(5).Value = "Nam") Then
                    rboNam.Checked = True
                ElseIf (DataGrid1.SelectedRows(0).Cells(5).Value = "Nu") Then
                    rboNu.Checked = True
                End If
                dtpNgaySinh.Value = DataGrid1.SelectedRows(0).Cells(6).Value
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub
End Class